package gemini.castor.ui.client.page.content.account.mydownline.form;

import gemini.basic.model.Distributor;
import gemini.castor.ui.client.utils.GuiUtils;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class MyDownlineFormView extends Composite implements
		MyDownlineFormPresenter.Display {

	@UiTemplate("MyDownlineFormView.ui.xml")
	interface MyUiBinder extends UiBinder<Panel, MyDownlineFormView> {
	}

	private static final MyUiBinder binder = GWT.create(MyUiBinder.class);

	public MyDownlineFormView() {
		initWidget(binder.createAndBindUi(this));
	}

	@UiField
	SimplePanel pnlMyDownline;

	@UiField
	HTML htmPhone;

	@UiField
	HTML htmDistributorID;

	@UiField
	HTML htmFullName;
	
	@UiField 
	HTML htmNoDownline;
	
	@UiField
	VerticalPanel pnlDownlineInfo;
	 

	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public void loadData() {
		if(myDownlineList.getDistributors()!=null && !myDownlineList.getDistributors().isEmpty() ){
			pnlDownlineInfo.setVisible(true);
			htmNoDownline.setVisible(false);
			
			Tree staticTree = createStaticTree();
			staticTree.setAnimationEnabled(true);
			staticTree.addSelectionHandler(new SelectionHandler<TreeItem>() {
				
				@Override
				public void onSelection(SelectionEvent<TreeItem> event) {
					reloadInfo("VN" + GuiUtils.getStringBetweenFirst(event
							.getSelectedItem().getHTML(), "VN", "-"));
				}
			});
			
			ScrollPanel staticTreeWrapper = new ScrollPanel(staticTree);
			staticTreeWrapper.setSize("300px", "300px");
			
			pnlMyDownline.clear();
			pnlMyDownline.add(staticTreeWrapper);
		}else{
			pnlDownlineInfo.setVisible(false);
			htmNoDownline.setVisible(true);
		}
	}

	private void showDistributorInfo(Distributor dis) {
		htmPhone.setHTML(dis.getPersons().get(0).getPhoneNumber());
		htmDistributorID.setHTML(dis.getCode());
		String fullName = dis.getPersons().get(0).getFirstName() + " "
				+ dis.getPersons().get(0).getLastName();
		htmFullName.setHTML(fullName);

	}

	private void reloadInfo(String code) {

		// Check if seleted current distributor
		Distributor currentDis = myDownlineList.getDistributors().get(0)
				.getDistributor();
		if (currentDis.getCode().equals(code)) {
			showDistributorInfo(currentDis);
		} else {
			// Check with downline level 1
			for (Distributor distributor : myDownlineList.getDistributors()) {
				if (distributor.getCode().equals(code)) {
					showDistributorInfo(distributor);
					break;
				} else {
					// Check with downline level 2
					for (Distributor dis : distributor.getDistributors()) {
						if (dis.getCode().equals(code)) {
							showDistributorInfo(dis);
							break;
						}
					}
				}
			}
		}
	}

	private Tree createStaticTree() {
		Tree tree = new Tree();
		HTML htm = new HTML(getDisSummary(myDownlineList.getDistributors()
				.get(0).getDistributor()));
		TreeItem rootTree = new TreeItem(htm);
		for (Distributor distritubor : myDownlineList.getDistributors()) {
			HTML htmNode = new HTML(getDisSummary(distritubor));
			TreeItem nodTree = new TreeItem(htmNode);
			if (distritubor.getDistributors().size() != 0) {
				for (Distributor downlineOfDistributor : distritubor
						.getDistributors()) {
					HTML htmChildNode = new HTML(
							getDisSummary(downlineOfDistributor));
					TreeItem childNode = new TreeItem(htmChildNode);
					nodTree.addItem(childNode);
				}
			}
			rootTree.addItem(nodTree);
		}
		tree.addItem(rootTree);

		return tree;
	}

	private String getDisSummary(Distributor dis) {
		String result = dis.getCode() + "- "
				+ dis.getPersons().get(0).getFirstName() + " "
				+ dis.getPersons().get(0).getLastName();

		return result;

	}

	private MyDownlineInfoFormModel myDownlineList;

	@Override
	public void setModel(MyDownlineInfoFormModel downlineList) {
		this.myDownlineList = downlineList;

	}
}
